home *** CD-ROM | disk | FTP | other *** search
/ Acorn RISC PD-CD 1 / Acorn RISC PD-CD 1.iso / utilities / _graphics / graphics / _jpeg / c / jwrppm < prev   
Encoding:
Text File  |  1991-10-28  |  4.2 KB  |  168 lines

  1. /*
  2.  * jwrppm.c
  3.  *
  4.  * Copyright (C) 1991, Thomas G. Lane.
  5.  * This file is part of the Independent JPEG Group's software.
  6.  * For conditions of distribution and use, see the accompanying README file.
  7.  *
  8.  * This file contains routines to write output images in PPM format.
  9.  * The PBMPLUS library is required (well, it will be in the real version).
  10.  *
  11.  * These routines may need modification for non-Unix environments or
  12.  * specialized applications.  As they stand, they assume output to
  13.  * an ordinary stdio stream.
  14.  *
  15.  * These routines are invoked via the methods put_pixel_rows, put_color_map,
  16.  * and output_init/term.
  17.  */
  18.  
  19. #include "jinclude.h"
  20.  
  21. #ifdef PPM_SUPPORTED
  22.  
  23.  
  24. static JSAMPARRAY color_map;    /* saves color map passed by quantizer */
  25.  
  26.  
  27. /*
  28.  * Write the file header.
  29.  */
  30.  
  31. METHODDEF void
  32. output_init (decompress_info_ptr cinfo)
  33. {
  34.   if (cinfo->out_color_space == CS_GRAYSCALE) {
  35.     /* emit header for raw PGM format */
  36.     fprintf(cinfo->output_file, "P5\n%ld %ld\n%d\n",
  37.             cinfo->image_width, cinfo->image_height, 255);
  38.   } else if (cinfo->out_color_space == CS_RGB) {
  39.     /* emit header for raw PPM format */
  40.     fprintf(cinfo->output_file, "P6\n%ld %ld\n%d\n",
  41.             cinfo->image_width, cinfo->image_height, 255);
  42.   } else {
  43.     ERREXIT(cinfo->emethods, "PPM output must be grayscale or RGB");
  44.   }
  45. }
  46.  
  47.  
  48. /*
  49.  * Write some pixel data.
  50.  */
  51.  
  52. METHODDEF void
  53. put_pixel_rows (decompress_info_ptr cinfo, int num_rows,
  54.                 JSAMPIMAGE pixel_data)
  55. {
  56.   register FILE * outfile = cinfo->output_file;
  57.   register JSAMPROW ptr0, ptr1, ptr2;
  58.   register long col;
  59.   register long width = cinfo->image_width;
  60.   register int row;
  61.   
  62.   if (cinfo->out_color_space == CS_GRAYSCALE) {
  63.     for (row = 0; row < num_rows; row++) {
  64.       ptr0 = pixel_data[0][row];
  65.       for (col = width; col > 0; col--) {
  66.         putc(GETJSAMPLE(*ptr0), outfile);
  67.         ptr0++;
  68.       }
  69.     }
  70.   } else {
  71.     for (row = 0; row < num_rows; row++) {
  72.       ptr0 = pixel_data[0][row];
  73.       ptr1 = pixel_data[1][row];
  74.       ptr2 = pixel_data[2][row];
  75.       for (col = width; col > 0; col--) {
  76.         putc(GETJSAMPLE(*ptr0), outfile);
  77.         ptr0++;
  78.         putc(GETJSAMPLE(*ptr1), outfile);
  79.         ptr1++;
  80.         putc(GETJSAMPLE(*ptr2), outfile);
  81.         ptr2++;
  82.       }
  83.     }
  84.   }
  85. }
  86.  
  87.  
  88. /*
  89.  * Write some pixel data when color quantization is in effect.
  90.  */
  91.  
  92. METHODDEF void
  93. put_demapped_rows (decompress_info_ptr cinfo, int num_rows,
  94.                    JSAMPIMAGE pixel_data)
  95. {
  96.   register FILE * outfile = cinfo->output_file;
  97.   register JSAMPROW ptr;
  98.   register long col;
  99.   register long width = cinfo->image_width;
  100.   register int row;
  101.   
  102.   if (cinfo->out_color_space == CS_GRAYSCALE) {
  103.     for (row = 0; row < num_rows; row++) {
  104.       ptr = pixel_data[0][row];
  105.       for (col = width; col > 0; col--) {
  106.         putc(GETJSAMPLE(color_map[0][GETJSAMPLE(*ptr)]), outfile);
  107.         ptr++;
  108.       }
  109.     }
  110.   } else {
  111.     for (row = 0; row < num_rows; row++) {
  112.       ptr = pixel_data[0][row];
  113.       for (col = width; col > 0; col--) {
  114.         register int pixval = GETJSAMPLE(*ptr);
  115.  
  116.         putc(GETJSAMPLE(color_map[0][pixval]), outfile);
  117.         putc(GETJSAMPLE(color_map[1][pixval]), outfile);
  118.         putc(GETJSAMPLE(color_map[2][pixval]), outfile);
  119.         ptr++;
  120.       }
  121.     }
  122.   }
  123. }
  124.  
  125.  
  126. /*
  127.  * Write the color map.
  128.  * For PPM output, we just demap the output data!
  129.  */
  130.  
  131. METHODDEF void
  132. put_color_map (decompress_info_ptr cinfo, int num_colors, JSAMPARRAY colormap)
  133. {
  134.   color_map = colormap;         /* save for use in output */
  135.   cinfo->methods->put_pixel_rows = put_demapped_rows;
  136. }
  137.  
  138.  
  139. /*
  140.  * Finish up at the end of the file.
  141.  */
  142.  
  143. METHODDEF void
  144. output_term (decompress_info_ptr cinfo)
  145. {
  146.   /* No work except to make sure we wrote the output file OK */
  147.   fflush(cinfo->output_file);
  148.   if (ferror(cinfo->output_file))
  149.     ERREXIT(cinfo->emethods, "Output file write error");
  150. }
  151.  
  152.  
  153. /*
  154.  * The method selection routine for PPM format output.
  155.  * This should be called from d_ui_method_selection if PPM output is wanted.
  156.  */
  157.  
  158. GLOBAL void
  159. jselwppm (decompress_info_ptr cinfo)
  160. {
  161.   cinfo->methods->output_init = output_init;
  162.   cinfo->methods->put_color_map = put_color_map;
  163.   cinfo->methods->put_pixel_rows = put_pixel_rows;
  164.   cinfo->methods->output_term = output_term;
  165. }
  166.  
  167. #endif /* PPM_SUPPORTED */
  168.